﻿if (!isGmail)
{
    var remoteRequest = function ()
    {
        var settings = arguments[0];
        settings.dataType = settings.contentType == "JSON" ? 'json' : "";
        if (settings.contentType != 'application/json')
            delete settings.contentType;

        var csrfName = $('#csrf input').attr("name");
        var csrfVal = $('#csrf input').val();
        if (!settings.data)
            settings.data = stringFormat("{0}={1}", csrfName, encodeURIComponent(csrfVal));
        else if ($.type(settings.data) == "string")
            settings.data += "&" + csrfName + "=" + encodeURIComponent(csrfVal);
        else settings.data[csrfName] = csrfVal;


        var old = settings.success;
        var success = function (data)
        {
            if (data && data.RedirectUrl)
                window.location = data.RedirectUrl;
            else
            {
                if (settings.dataType == 'json' && !(data && data.NoEncoding))
                {
                    escapeObject(data);
                }
                if (old)
                    old(data);
            }
        };
        settings.error = function (jqXHR, textStatus, errorThrown)
        {
            debugger;
            //alert(textStatus);
        };
        settings.success = success;
        $.ajax.call(this, settings);
    };
    var adjustHeight = function (minW, minH)
    {
        for (var i = 1; i < 12; i += 2)
            window.setTimeout(function ()
            {
                ChangeHeight(Math.max($('body').height() + 60, minH));
                ChangeSize(minW || 600, Math.max($('body').height() + 60, minH));
            }, i * 100);
    };
    var encodeValues = function (el)
    {
        return el;
    };
    var global = { matches: [] };

    var viewTaskInClarizen = function (id, url)
    {
        if(!url)
        url = id ?
            stringFormat("/Clarizen/View/?id={0}&type={1}", id, "Task") :
            "/Clarizen/Pages/MainPage/WorkItems.aspx";

        window.open(baseUrl + "/Clarizen/Link?link=" + escape(url));

    };

    var getDocuments
}

var logout = function () {

    try {
        if (typeof GetInfo != "undefined" && GetInfo())
        StoreGlobalKey("accessToken", "");
        }
        catch (er)
        {}

    remoteRequest({
        url: baseUrl + "/login/logout?goto=" + escape(window.location),
        contentType: "JSON",
        type: "POST",
        data: null
    });
};

function escapeObject(ob)
{
    if ($.type(ob) == "object")
        for (key in ob)
        {
            if (key != "ErrorMessage")
                ob[key] = escapeObject(ob[key]);
        }
    else if ($.type(ob) == "array")
        for (var i = 0; i < ob.length; i++)
            ob[i] = escapeObject(ob[i])
    else if ($.type(ob) == "string")
        return htmlEscape(ob);

    return ob;
}

function htmlEscape(str)
{
    if ($.type(str) == "string")
        return str.replace(/&/g, "&amp;").replace(/</g, '&lt;').replace(/>/g, '&gt;');
    return str;
}

function htmlUnEscape(str)
{
    if ($.type(str) == "string")
        return str.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, "&");
    return str;
}

function getCSRF()
{
    return {
        name:$('#csrf input').attr("name"),
        val:$('#csrf input').val()
    }
}